<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @changeForm="getList(1)"
      @clear="clear"
    />
    <div v-if="meta.indexOf('ShopPickUpAdd') > -1" class="doumee_table_header">
      <el-button
        v-preventReClick
        type="primary"
        @click="toEdit()"
      >新增</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" label="自提点名称" prop="name" show-overflow-tooltip min-width="120" />
      <el-table-column align="center" label="联系电话" prop="phone" show-overflow-tooltip min-width="120" />
      <el-table-column align="center" label="营业时间" prop="openTime" show-overflow-tooltip min-width="120" />
      <el-table-column align="center" label="自提地址" prop="address" show-overflow-tooltip min-width="120" />
      <el-table-column align="center" label="经纬度" prop="lonAndLat" show-overflow-tooltip min-width="120" />
      <el-table-column align="center" label="创建时间" prop="createTime" show-overflow-tooltip min-width="160" />
      <el-table-column width="1" />
      <el-table-column
        align="center"
        label="状态"
        min-width="100"
        fixed="right"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="'1'"
            :inactive-value="'0'"
            :disabled="meta.indexOf('ShopPickUpUpdateStatus') == -1"
            @change="stateChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('ShopPickUpUpdate') > -1 && scope.row.status == '1'"
              v-preventReClick
              type="text"
              @click="toEdit(scope.row.id)"
            >编辑</el-button>
            <el-button
              v-if="meta.indexOf('ShopPickUpUpdate') > -1 && scope.row.status == '0'"
              v-preventReClick
              type="text"
              style="color: red"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Edit
      v-if="isShowEdit"
      ref="EditRef"
      @close="isShowEdit = false"
      @success="getList"
    />
  </div>
</template>

<script>
import { shopPickUpAddressPageList, shopPickUpAddressUpdateStatus, shopPickUpAddressDel } from '@/api'
import { parseTime } from '@/utils/index'
import { queryFormConfig } from './config'
import Pagination from '@/components/paginations'
import Edit from './edit.vue'
export default {
  name: 'ShopPickUp',
  components: {
    Pagination,
    Edit
  },
  data() {
    return {
      isShowEdit: false,
      modalDialog: this.$store.state.app.dialog,
      queryFormConfig,
      list: [],
      listLoading: true,
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      meta: [],
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    // 删除
    toDelete(id) {
      const that = this
      this.$confirm('此操作将删除该自提点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          shopPickUpAddressDel({ param: { id }}).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success('删除成功')
            that.getList()
          })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    },
    // 获取场馆列表
    getList(page) {
      // 获取会员列表
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      shopPickUpAddressPageList({
        pagination,
        param: { ...filters }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.total = res.totalCount
            if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
              this.getList(Math.ceil(res.totalCount / pagination.rows))
            }
          }
        })
        .catch((err) => {
          console.log(err)
          this.listLoading = false
        })
    },
    // 编辑场馆
    toEdit(id) {
      this.isShowEdit = true
      this.$nextTick(() => {
        this.$refs.EditRef.isShowModal = true
        if (id) {
          this.$refs.EditRef.id = id
          this.$refs.EditRef.mainName = '编辑自提点'
          this.$refs.EditRef.getVenueInfo(id)
        } else {
          this.$refs.EditRef.mainName = '新增自提点'
          this.$refs.EditRef.venueInfo = {}
        }
      })
    },

    stateChange(v) {
      const obj = { ...v }
      const arr = this.list
      const that = this
      this.listLoading = true
      shopPickUpAddressUpdateStatus({
        param: {
          id: obj.id,
          status: obj.status
        }
      })
        .then(res => {
          if (res.errorCode === '000000') {
            if (obj.openStatus === '0') {
              that.$message.success('禁用成功')
            } else {
              that.$message.success('启用成功')
            }
            that.getList(1)
          } else {
            arr.forEach(e => {
              if (e.id === obj.id) {
                e.openStatus = obj.openStatus === '1' ? '0' : '1'
              }
            })
            that.list = arr
            that.listLoading = false
          }
        }).catch(() => {
          arr.forEach(e => {
            if (e.id === obj.id) {
              e.openStatus = obj.openStatus === '1' ? '0' : '1'
            }
          })
          that.list = arr
          that.listLoading = false
        })
    },
    // 清空
    clear() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>
